package com.anxin.controller;

import com.anxin.common.Result;
import com.anxin.converter.DepartmentConverter;
import com.anxin.entity.Department;
import com.anxin.entity.excel.DepartmentExcel;
import com.anxin.service.DepartmentService;
import com.anxin.service.ExcelService;
import com.anxin.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Tag(name = "科室管理")
@RestController
@RequestMapping("/api/department")
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService departmentService;
    
    @Autowired
    private ExcelService excelService;
    
    @Autowired
    private FileService fileService;
    
    @Autowired
    private DepartmentConverter departmentConverter;

    @Operation(summary = "导入科室Excel数据")
    @PostMapping("/import")
    public Result<String> importExcel(@Parameter(description = "Excel文件") @RequestParam("file") MultipartFile file) {
        try {
            // 导入Excel数据
            List<DepartmentExcel> excelList = excelService.importExcel(file, DepartmentExcel.class);
            // 转换为实体类
            List<Department> departmentList = departmentConverter.toEntityList(excelList);
            // 保存数据
            departmentService.saveBatch(departmentList);
            return Result.success("导入成功");
        } catch (Exception e) {
            return Result.error("导入失败：" + e.getMessage());
        }
    }

    @GetMapping("/export")
    public Result<String> exportExcel() {
        try {
            // 查询所有数据
            List<Department> departmentList = departmentService.list();
            // 转换为Excel对象
            List<DepartmentExcel> excelList = departmentConverter.toExcelList(departmentList);
            // 导出Excel
            String fileName = excelService.exportExcel(excelList, DepartmentExcel.class);
            return Result.success(fileName);
        } catch (Exception e) {
            return Result.error("导出失败：" + e.getMessage());
        }
    }

    @GetMapping("/download/{fileName}")
    public byte[] downloadFile(@PathVariable String fileName) {
        try {
            return fileService.downloadFile(fileName);
        } catch (Exception e) {
            throw new RuntimeException("下载文件失败", e);
        }
    }

    @GetMapping("/{id}")
    public Result<Department> getById(@PathVariable String id) {
        return Result.success(departmentService.getById(id));
    }

    @GetMapping("/list")
    public Result<List<Department>> list() {
        return Result.success(departmentService.list());
    }

    @PostMapping("/save")
    public Result<Boolean> save(@RequestBody Department department) {
        return Result.success(departmentService.saveOrUpdate(department));
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable String id) {
        return Result.success(departmentService.removeById(id));
    }

    /**
     * 获取科室树状结构（无需登录，带缓存）
     */
    @GetMapping("/tree")
    public Result<List<Department>> getDepartmentTree() {
        return Result.success(departmentService.getDepartmentTree());
    }
}
